package com.qingke.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingke.annotation.SkipSecurityCheck;
import com.qingke.pojo.Classes;
import com.qingke.pojo.Employee;
import com.qingke.pojo.EmployeeAccount;
import com.qingke.pojo.InterviewAbility;
import com.qingke.pojo.Student;
import com.qingke.service.ClassService;
import com.qingke.service.EmployeeService;
import com.qingke.service.InterviewAbilityService;
import com.qingke.service.StudentService;

@Controller
@RequestMapping("/guider")
public class GuiderController {
	private static Logger logger = Logger.getLogger(GuiderController.class);
	private Map<String, Object> map;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private ClassService classService;
	@Autowired
	private InterviewAbilityService interviewAbilityService;

	@RequestMapping("getGuider")
	@SkipSecurityCheck
	@ResponseBody
	public Map getGuider(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee guider = (Employee) se.getAttribute("user");
		logger.info("get guider:" + guider);
		map.put("guider", guider);
		return map;
	}

	@RequestMapping("updateGuiderMessage")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateGuiderMessage(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee guider = (Employee) se.getAttribute("user");
		String userId = request.getParameter("user_id");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		logger.info("get guiderMessage[" + "userId=" + userId + ",email=" + email + ",phone=" + phone + "]");
		if (!userId.equals(guider.getEmployeeNumber())) {
			map.put("isSame", "no");
		} else {
			guider.setEmail(email);
			guider.setPhone(phone);
			int count = employeeService.updateEmployeeByPrimaryKey(guider);
			if (count > 0) {
				map.put("update", "success");
			} else {
				map.put("update", "failed");
			}
		}
		Collection<Object> c = map.values();
		Iterator it = c.iterator();
		for (; it.hasNext();) {
			System.out.println(it.next());
		}
		return map;
	}

	@RequestMapping("updateGuiderPassword")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateGuiderPassword(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String oldPassword = request.getParameter("old_password");
		String newPassword = request.getParameter("new_password");
		String confrimPassword = request.getParameter("confirm_password");
		logger.info("get guiderPassword:old_password" + oldPassword + ",new_password=" + newPassword
				+ ",confirm_password=" + confrimPassword);
		HttpSession se = request.getSession();
		Employee guider = (Employee) se.getAttribute("user");
		EmployeeAccount employeeAccount = employeeService.getEmployeeAccountById(guider.getId());
		if (!oldPassword.equals(employeeAccount.getPassword())) {
			map.put("isExsit", "false");
		} else if (!newPassword.equals(confrimPassword)) {
			map.put("equal", "false");
		} else {
			employeeAccount.setPassword(newPassword);
			int count = employeeService.updateEmployeeAccountByPrimaryKey(employeeAccount);
			if (count > 0) {
				map.put("update", "success");
			} else {
				map.put("update", "false");
			}
		}
		Collection<Object> c = map.values();
		Iterator it = c.iterator();
		for (; it.hasNext();) {
			System.out.println(it.next());
		}
		return map;
	}

	@RequestMapping("listClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map listClass(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		List<Classes> clazz = classService.listClass();
		List<Classes> classes = new ArrayList();
		if (!clazz.isEmpty()) {
			for (Classes clas : clazz) {
				if (!clas.getIsDeleted()) {
					logger.info("get class=" + clas);
					classes.add(clas);
				}
			}
			map.put("classes", classes);
		} else {
			map.put("classes", "");
		}
		return map;
	}

	@RequestMapping("listStudentByClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map listStudentByClass(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String className = request.getParameter("className");
		logger.info("get className=" + className);
		List<Student> student = new ArrayList();
		List<Student> students = studentService.listStudent();
		if (!students.isEmpty()) {
			if (!"".equals(className)) {
				for (Student st : students) {
					logger.info("get student=" + st + ",className=" + st.getClazz().getClassName());
					if (!st.getIsDeleted() && st.getClazz().getClassName().equals(className)) {
						student.add(st);
					}
					map.put("student", student);
				}
			} else {
				map.put("student", "");
			}
		} else {
			map.put("student", "");
		}
		return map;
	}

	@RequestMapping("listStudentInterviewAbility")
	@SkipSecurityCheck
	@ResponseBody
	public Map listStudentInterviewAbility(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String studentName = request.getParameter("student_name");
		logger.info("get studentName=" + studentName);
		Student student = studentService.getStudentByStudentName(studentName);
		if (student != null) {
			List<InterviewAbility> interviewAbility = new ArrayList();
			List<InterviewAbility> interviewAbilitys = interviewAbilityService
					.listInterviewAbilityByStudentId(student.getId());
			if (!interviewAbilitys.isEmpty()) {
				for (InterviewAbility ia : interviewAbilitys) {
					logger.info("list interviewAbility:" + ia);
					if (!ia.getIsDeleted()) {
						interviewAbility.add(ia);
					}
				}
				map.put("interviewAbility", interviewAbility);
			} else {
				map.put("interviewAbility", "");
			}
		} else {
			map.put("interviewAbility", "");
		}
		return map;
	}

	@RequestMapping("deleteInterviewAbility")
	@SkipSecurityCheck
	@ResponseBody
	public Map deleteInterviewAbility(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		int id = Integer.valueOf(request.getParameter("id"));
		logger.info("get interviewAbility_id=" + id);
		int count = interviewAbilityService.softDelete(id);
		if (count > 0) {
			map.put("isDelete", "success");
		} else {
			map.put("isDelete", "failed");
		}
		return map;
	}

	@RequestMapping("getInterviewAbility")
	@SkipSecurityCheck
	@ResponseBody
	public Map getInterviewAbility(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		int id = Integer.valueOf(request.getParameter("id"));
		logger.info("get interviewAbility_id=" + id);
		InterviewAbility interviewAbility = interviewAbilityService.getInterviewAbilityById(id);
		se.setAttribute("interviewAbility", interviewAbility);
		map.put("interviewAbility", interviewAbility);
		return map;
	}

	@RequestMapping("updateStudentInterviewAbility")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateStudentInterviewAbility(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee guider = (Employee) se.getAttribute("user");
		InterviewAbility interviewAbility = (InterviewAbility) se.getAttribute("interviewAbility");
		String time = request.getParameter("time");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long lt = new Long(time);
		Date d = new Date(lt);
		String res = format.format(d);
		Date date = null;
		try {
			date = format.parse(res);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String comment = request.getParameter("comment");
		logger.info("get date=" + date + ",comment=" + comment);
		if (!interviewAbility.getDate().equals(date)) {
			map.put("isSame", "no");
		} else {
			interviewAbility.setDate(date);
			interviewAbility.setEmployeeId(guider.getId());
			interviewAbility.setComment(comment);
			int count = interviewAbilityService.updateInterviewAbility(interviewAbility);
			if (count > 0) {
				map.put("update", "success");
			} else {
				map.put("update", "failed");
			}
		}
		return map;
	}

	@RequestMapping("createStudentInterviewAbility")
	@SkipSecurityCheck
	@ResponseBody
	public Map createStudentInterviewAbility(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee guider = (Employee) se.getAttribute("user");
		String student_name = request.getParameter("student_name");
		String time = request.getParameter("time");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long lt = new Long(time);
		Date d = new Date(lt);
		String res = format.format(d);
		Date date = null;
		try {
			date = format.parse(res);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String comment = request.getParameter("comment");
		logger.info("get studentName=" + student_name + ",date=" + date);
		Student student = studentService.getStudentByStudentName(student_name);
		if (student == null) {
			map.put("isExist", "no");
		} else {
			InterviewAbility interviewAbility = new InterviewAbility();
			interviewAbility.setStudentId(student.getId());
			interviewAbility.setDate(date);
			interviewAbility.setEmployeeId(guider.getId());
			interviewAbility.setComment(comment);
			interviewAbility.setIsDeleted(false);
			int count = interviewAbilityService.saveInterviewAbility(interviewAbility);
			if (count > 0) {
				map.put("create", "success");
			} else {
				map.put("create", "failed");
			}
		}
		return map;
	}
}
